package rhc.taskSchedule.command.impl;

import rhc.discribute.node.commander.Commander;
import rhc.discribute.node.host.Host;
import rhc.taskSchedule.BasicComponent;
import rhc.taskSchedule.command.RunningCommandCreater;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.common.CommandConstants;
import rhc.taskSchedule.entity.RunningCode;

import com.alibaba.fastjson.JSONObject;

/**命令获取或执行接口实现
 * @author rhc
 *
 */
public class RunningCommandCreaterImpl extends BasicComponent implements RunningCommandCreater {

	private TaskScheduleCommanderExecute taskScheduleCommanderExecute;
	
	public RunningCommandCreaterImpl(TaskScheduleCommanderExecute taskScheduleCommanderExecute){
		this.taskScheduleCommanderExecute=taskScheduleCommanderExecute;
	}
	
	@Override
	public String getRunCommand(String runningId) {
		JSONObject commandJson=getCommon();
		
		JSONObject commandData=new JSONObject();
		commandData.put(CommandConstants.RUNNING_ID, runningId);
		commandData.put(CommandConstants.RUNNING_CODE, RunningCode.RUN.code);
		
		commandJson.put(CommandConstants.COMMAND_DATA, commandData);
		
		return commandJson.toJSONString();
	}

	@Override
	public String getStopCommand(String runningId) {
		JSONObject commandJson=getCommon();
		
		JSONObject commandData=new JSONObject();
		commandData.put(CommandConstants.RUNNING_ID, runningId);
		commandData.put(CommandConstants.RUNNING_CODE, RunningCode.STOP.code);
		
		commandJson.put(CommandConstants.COMMAND_DATA, commandData);
		
		return commandJson.toJSONString();
	}

	@Override
	public String getPauseCommand(String runningId) {
		JSONObject commandJson=getCommon();
		
		JSONObject commandData=new JSONObject();
		commandData.put(CommandConstants.RUNNING_ID, runningId);
		commandData.put(CommandConstants.RUNNING_CODE, RunningCode.PAUSE.code);
		
		commandJson.put(CommandConstants.COMMAND_DATA, commandData);
		
		return commandJson.toJSONString();
	}

	@Override
	public String getRegainCommand(String runningId) {
		JSONObject commandJson=getCommon();
		
		JSONObject commandData=new JSONObject();
		commandData.put(CommandConstants.RUNNING_ID, runningId);
		commandData.put(CommandConstants.RUNNING_CODE, RunningCode.REGAIN.code);
		
		commandJson.put(CommandConstants.COMMAND_DATA, commandData);
		
		return commandJson.toJSONString();
	}

	@Override
	public boolean sendCommand(String command,Host host) throws Exception {
		Commander distributeNodeCommander=taskScheduleCommanderExecute.getDistributeNodeCommander();
		
		SendMsgCallback callback=new SendMsgCallback();
		
		distributeNodeCommander.getConnecter().sendMessageToHost(host, command, callback, true);
		
		return callback.sendSuccess();
	}
	
	/**获取公共部分
	 * @return
	 */
	private JSONObject getCommon(){
		JSONObject runCommand=new JSONObject();
		runCommand.put(CommandConstants.COMMAND_CODE, CommandConstants.RUNNING_COMMAND_CODE);
		
		return runCommand;
	}
	
}
